package Thread;
class Counter2 {
    private int count = 0;

    public void add() {
        synchronized (this) {
            count++;
        }
    }

    public int get() {
        return count;
    }
}
//如何实现可重入锁
//1，在锁内部记录是哪个线程拥有锁，后续每次加锁都进行判定
//2，通过计数器记录加锁的次数，从而正确解锁

public class Demo19 {
    public static void main(String[] args) throws InterruptedException {
        Counter2 counter = new Counter2();
        Thread t1 = new Thread(()->{
        synchronized (counter) {
            synchronized (counter) {
                synchronized (counter) {
                    counter.add();
                }
            }
        } //加锁解锁都在最外层的synchronized
        });
        t1.start();
        t1. join();
        System.out.println(counter.get());
    }
}

